//
//  BInviteManagerViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/7/8.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BInviteManagerViewController.h"

@interface BInviteManagerViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UIButton *inviteB;

@end

@implementation BInviteManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"被邀请伙伴信息";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    
    _inviteB.layer.cornerRadius = 5;
}


- (IBAction)invite:(id)sender {
    
    
    if (![self.phoneTF.text isValidMobileNumber]) {
        ShowToastWithText(@"请输入正确格式的手机号");
        return;
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"phone"] = _phoneTF.text;
    
    
    [NetWorkTool POST:invitationAgentLevelB2 param:param success:^(id dic) {
        
        ShowToastWithText(@"添加成功");
        [self share];
        
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
    
}



- (void)share{
    
    NSMutableArray *titlearr     = [NSMutableArray arrayWithCapacity:5];
    NSMutableArray *imageArr     = [NSMutableArray arrayWithCapacity:5];
    NSMutableArray *typeArr     = [NSMutableArray arrayWithCapacity:5];
    [titlearr addObjectsFromArray:@[@"微信", @"微信朋友圈"]];
    [imageArr addObjectsFromArray:@[@"weixin22",@"weixin11"]];
    
    ShareView *shareView = [[ShareView alloc] initWithShareHeadOprationWith:titlearr andImageArry:imageArr andProTitle:@"分享到"];
    [shareView setBtnClick:^(NSInteger btnTag) {
        NSLog(@"\n点击第几个====%d\n当前选中的按钮title====%@",(int)btnTag,titlearr[btnTag]);
        [self shareWitipe:btnTag];
    }];
    [[UIApplication sharedApplication].keyWindow addSubview:shareView];
    
    
    
    
}


-(void)shareWitipe:(NSInteger )ind{
    ind++;
    //    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *typestr = ind ==0?@"JSHAREPlatformWechatSession":@"JSHAREPlatformWechatTimeLine";
    
    NSArray *type = @[@"JSHAREPlatformWechatSession",@"JSHAREPlatformWechatTimeLine"];
    
    JSHAREMessage *message = [JSHAREMessage message];
    message.mediaType = JSHARELink;
    NSString *invitation_code =[NSString stringWithFormat:@"%@",[USER_DEFAULT objectForKey:@"invitation_code"]];
    //    NSString *invitUrl = [NSString stringWithFormat:@"https://www.zzzsyh.com/index.html#/B_joining?invitationCode=%@", invitation_code];
    //    NSLog(@"%@", invitUrl);
    message.url = @"https://www.zzzsyh.com/index.html#/A_Joining";
    message.text = [NSString stringWithFormat:@"侯保师傅接单APP注册:%@",[self localizedStringTime]];
    message.title = @"主事丫环电器维修服务平台邀您一起赚钱";
    message.platform = ind;
//    NSString *imageURL = @"http://img2.3lian.com/2014/f5/63/d/23.jpg";
//    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
//    message.image = imageData;
    
    NSString *imageStr = @"shifuduan";
    NSData *imageData = [imageStr dataUsingEncoding:NSUTF8StringEncoding];
    message.image = imageData;
    [JSHAREService share:message handler:^(JSHAREState state, NSError *error) {
        //        [self showAlertWithState:state error:error];
        KMyLog(@"sdf%@",error);
        //            [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (!error) {
            ShowToastWithText(@"分享成功");
        }else{
            ShowToastWithText(@"分享失败");
            
            NSLog(@"分享失败, error : %@", error);
        }
    }];
    
}
- (NSString *)localizedStringTime{
    NSDateFormatter*formatter = [[NSDateFormatter alloc]init];[formatter setDateFormat:@"yyy-MM-dd HH:mm:ss"];
    NSString*dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}

@end
